package net.frenopatico.citadels.conf;

import java.util.HashMap;
import java.util.Map;

import net.frenopatico.citadels.app.CitadelsApplication;
import net.frenopatico.citadels.app.GuiceApplicationServlet;
import net.frenopatico.citadels.modules.AppEngineModule;
import net.frenopatico.citadels.modules.ManagersModule;
import net.frenopatico.citadels.modules.ServicesModule;
import net.frenopatico.citadels.modules.WindowsModule;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.servlet.GuiceServletContextListener;
import com.google.inject.servlet.ServletModule;
import com.google.inject.servlet.ServletScopes;
import com.vaadin.Application;

/**
 * Configure the Guice Servlet.
 *
 */
public class CitadelsServletConfig extends GuiceServletContextListener {

    @Override
    protected Injector getInjector() {

        final Module gaeModule = AppEngineModule.build().withAsyncDatastoreService().withMemcacheService();
        final Module windowsModule = WindowsModule.build();
        final Module servicesModule = ServicesModule.build();
        final Module managersModule = ManagersModule.build();

        final ServletModule module = new ServletModule() {
            @Override
            protected void configureServlets() {
                bind( Application.class ).to( CitadelsApplication.class ).in( ServletScopes.SESSION );

                Map<String, String> parameters = new HashMap<String, String>( 1 );
                parameters.put( "application", CitadelsApplication.class.getName() );

                serve( "/app/*", "/VAADIN/*", "/UIDL*" ).with( GuiceApplicationServlet.class, parameters );
            }
        };

        final Injector injector = Guice.createInjector( module, managersModule, windowsModule, servicesModule, gaeModule );

        return injector;
    }
}
